/* -----------------------------------------------------------------------------
*  CFileNameHelper
*  Author: Antonio Javier Gallego Sánchez (ajgallego AT gmail DOT com)
*  License: GNU Public License
* ----------------------------------------------------------------------------*/

#include "CFileNameHelper.hpp"

#include <stdio.h>
#include <string>
#include <string.h>
#include "asrtbas.h"
using namespace std;

//------------------------------------------------------------------------------
//
const char* CFileNameHelper :: getPathName( const char* filename )
{
    if( strlen( filename ) == 0 )
        return "";

    string strFilename( filename );

    size_t pos = strFilename.find_last_of("/");

    if( pos != string::npos )
        return strdup( strFilename.substr( 0, pos + 1 ).c_str() );
    else
        return "";
}

//------------------------------------------------------------------------------
//
const char* CFileNameHelper :: getFileName( const char* filename )
{
    if( strlen( filename ) == 0 )
        return "";

    string strFilename( filename );

    size_t pos = strFilename.find_last_of("/");

    if( pos != string::npos )
        return strdup( strFilename.substr( pos + 1 ).c_str() );
    else
        return filename;
}

//------------------------------------------------------------------------------
//
const char* CFileNameHelper :: getFileNameWithoutExtension( const char* filename )
{
    string onlyFilename = CFileNameHelper::getFileName( filename );

    size_t pos = onlyFilename.find_last_of(".");

    if( pos != string::npos )
        return strdup( onlyFilename.substr( 0, pos ).c_str() );
    else
        return strdup( onlyFilename.c_str() );
}

//------------------------------------------------------------------------------
// Returns file extensions WITH the point (Ex: .txt)
const char* CFileNameHelper :: getExtension( const char* filename )
{
    string strFilename( filename );

    size_t pos = strFilename.find_last_of(".");

    if( pos != string::npos )
        return strdup( strFilename.substr( pos ).c_str() );
    else
        return "";
}

//------------------------------------------------------------------------------
//
const char* CFileNameHelper :: addFinalSlashBar( const char* foldername )
{
    string r_value( foldername );

    if( foldername[ strlen( foldername ) - 1 ] != '/' )
    {
        r_value += "/";
    }

    return strdup( r_value.c_str() );
}

//------------------------------------------------------------------------------
//
const char* CFileNameHelper :: generateNumberedFilename( const char* path,
                        const char* filename,
                        unsigned int numeration, bool numberAsPrefix,
                        const char* separatorOfNumberAndName )
{
    char strNumeration[20];
    string generatedName;

    sprintf ( strNumeration, "%010d", numeration );

    if( numberAsPrefix )
    {
        generatedName = string( path )
                           + string( strNumeration )
                           + string( separatorOfNumberAndName )
                           + string( filename );
    }
    else  // Suffix...
    {
        generatedName = string( path )
                       + CFileNameHelper::getFileNameWithoutExtension( filename )
                       + string( strNumeration )
                       + string( separatorOfNumberAndName )
                       + CFileNameHelper::getExtension( filename );
    }

    return strdup( generatedName.c_str() );
}

/**
*/